﻿using System;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace CSharp.Common.URL
{
    public class UrlHelper
    {
        private static Encoding encoding = Encoding.UTF8;

        public static string AddParameter(string url, string paramName, string value)
        {
            string str;
            Uri uri = new Uri(url);
            if (string.IsNullOrEmpty(uri.Query))
            {
                str = HttpContext.Current.Server.UrlEncode(value);
                return (url + ("?" + paramName + "=" + str));
            }
            str = HttpContext.Current.Server.UrlEncode(value);
            return (url + ("&" + paramName + "=" + str));
        }

        public static string Base64Decrypt(string eStr)
        {
            if (!IsBase64(eStr))
            {
                return eStr;
            }
            byte[] bytes = Convert.FromBase64String(eStr);
            return HttpUtility.UrlDecode(encoding.GetString(bytes));
        }

        public static string Base64Encrypt(string sourthUrl)
        {
            string s = HttpUtility.UrlEncode(sourthUrl);
            return Convert.ToBase64String(encoding.GetBytes(s));
        }

        public static bool IsBase64(string eStr)
        {
            if ((eStr.Length % 4) != 0)
            {
                return false;
            }
            if (!Regex.IsMatch(eStr, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
            {
                return false;
            }
            return true;
        }

        public static void ParseUrl(string url, out string baseUrl, out NameValueCollection nvc)
        {
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            nvc = new NameValueCollection();
            baseUrl = "";
            if (url != "")
            {
                int index = url.IndexOf('?');
                if (index == -1)
                {
                    baseUrl = url;
                }
                else
                {
                    baseUrl = url.Substring(0, index);
                    if (index != (url.Length - 1))
                    {
                        string input = url.Substring(index + 1);
                        MatchCollection matchs = new Regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", RegexOptions.Compiled).Matches(input);
                        foreach (Match match in matchs)
                        {
                            nvc.Add(match.Result("$2").ToLower(), match.Result("$3"));
                        }
                    }
                }
            }
        }

        public static string UpdateParameter(string url, string paramName, string value)
        {
            string str = paramName + "=";
            int startIndex = url.IndexOf(str) + str.Length;
            int index = url.IndexOf("&", startIndex);
            if (index == -1)
            {
                url = url.Remove(startIndex, url.Length - startIndex);
                url = url + value;
                return url;
            }
            url = url.Remove(startIndex, index - startIndex);
            url = url.Insert(startIndex, value);
            return url;
        }
    }
}

